using System.Collections.Generic;
using PrimeTween;
using UnityEngine;
using World;

namespace Level
{
    public class EnemySpawner : MonoBehaviour
    {
        [SerializeField] private Transform enemyBasePrefab;

        private readonly List<Transform> _specialEnemyPrefabs = new();
        

        public void SpawnRandom(int count)
        {
            for (int i = 0; i < count; i++)
            {
                Transform enemyPrefab = PickEnemyType();

                float spawnRange = WorldInfo.Instance.WorldRadius + 1f;
                Vector3 position = Quaternion.Euler(0, 0, Random.Range(0f, 360f)) * new Vector3(spawnRange, 0f);
                SpawnEnemy(enemyPrefab, position);
            }
        }

        public void AddSpecialEnemyType(Transform prefab)
        {
            _specialEnemyPrefabs.Add(prefab);
        }

        private void SpawnEnemy(Transform enemyPrefab, Vector3 position)
        {
            Transform enemy = Instantiate(enemyPrefab, position, Quaternion.identity);
            Tween.Scale(enemy, 0f, enemy.localScale.x, 0.5f);
        }

        private Transform PickEnemyType()
        {
            if (_specialEnemyPrefabs.Count == 0 || Random.value <= 0.6f)
            {
                return enemyBasePrefab;
            }

            return _specialEnemyPrefabs[Random.Range(0, _specialEnemyPrefabs.Count)];
        }
    }
}